home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / prog / graphics / leda.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  5KB  |  200 lines

  1. #include <LEDA/graph.h>
  2. #include <LEDA/stream.h>
  3. #include <LEDA/array.h>
  4. #include <LEDA/window.h>
  5. #include <math.h>
  6.  
  7.  
  8. typedef GRAPH<vector,int> polyhedron;
  9.  
  10. void rotate(float alpha1,float alpha2, vector& p)
  11.   // rotate 3d-point p about the origin
  12.   // by alpha2 in yz-plane and alpha1 in xy-plane
  13.  
  14.     double R  = hypot(p[1],p[2]);
  15.     double phi = asin(p[1]/R);
  16.   
  17.     if (p[2] < 0) phi = M_PI - phi;
  18.   
  19.     p[1]  = ((R != 0) ? R*sin(phi+alpha2) : 0);
  20.     p[2]  = ((R != 0) ? R*cos(phi+alpha2) : 0);
  21.  
  22.     R   = hypot(p[0],p[2]);
  23.     phi = asin(p[0]/R);
  24.  
  25.     if (p[2] < 0) phi = M_PI - phi;
  26.   
  27.     p[0]  = ((R != 0) ? R*sin(phi+alpha1) : 0);
  28.     p[2]  = ((R != 0) ? R*cos(phi+alpha1) : 0);
  29. }
  30.  
  31.  
  32. inline point project(vector p)   // project p into xy-plane
  33. { return point(p[0],p[1]); }
  34.  
  35.  
  36. void draw_poly(window& W, polyhedron& poly, vector trans)
  37. { edge e;
  38.   forall_edges(e,poly) 
  39.   { point a = project(poly[source(e)] + trans );
  40.     point b = project(poly[target(e)] + trans );
  41.     W.draw_segment(a,b,blue);
  42.    }
  43.  }
  44.  
  45.  
  46. void make_side(polyhedron& poly, node* L, float z)
  47. {
  48.   L[0]= poly.new_node(vector(-70,-20,z));
  49.   L[1]= poly.new_node(vector(-40,-20,z));
  50.   L[2]= poly.new_node(vector(-40,-10,z));
  51.   L[3]= poly.new_node(vector(-60,-10,z));
  52.   L[4]= poly.new_node(vector(-60,+20,z));
  53.   L[5]= poly.new_node(vector(-70,+20,z));
  54.  
  55.   poly.new_edge(L[0],L[5]);
  56.   for(int i = 1; i<=5; i++) poly.new_edge(L[i], L[i-1]);
  57.  
  58.   L[6] = poly.new_node(vector(-30,-20,z));
  59.   L[7] = poly.new_node(vector( -5,-20,z));
  60.   L[8] = poly.new_node(vector( -5,-10,z));
  61.   L[9] = poly.new_node(vector(-20,-10,z));
  62.   L[10]= poly.new_node(vector(-20, -5,z));
  63.   L[11]= poly.new_node(vector( -5, -5,z));
  64.   L[12]= poly.new_node(vector( -5, +5,z));
  65.   L[13]= poly.new_node(vector(-20, +5,z));
  66.   L[14]= poly.new_node(vector(-20,+10,z));
  67.   L[15]= poly.new_node(vector( -5,+10,z));
  68.   L[16]= poly.new_node(vector( -5,+20,z));
  69.   L[17]= poly.new_node(vector(-30,+20,z));
  70.  
  71.   poly.new_edge(L[6],L[17]);
  72.   for(i = 7; i<=17; i++) poly.new_edge(L[i], L[i-1]);
  73.  
  74.   L[18]= poly.new_node(vector( 5,-20,z));
  75.   L[19]= poly.new_node(vector(20,-20,z));
  76.   L[20]= poly.new_node(vector(35,-10,z));
  77.   L[21]= poly.new_node(vector(35,+10,z));
  78.   L[22]= poly.new_node(vector(20,+20,z));
  79.   L[23]= poly.new_node(vector( 5,+20,z));
  80.  
  81.   poly.new_edge(L[18],L[23]);
  82.   for(i = 19; i<=23; i++) poly.new_edge(L[i], L[i-1]);
  83.  
  84.   L[24]= poly.new_node(vector(15,-10,z));
  85.   L[25]= poly.new_node(vector(20,-10,z));
  86.   L[26]= poly.new_node(vector(25, -5,z));
  87.   L[27]= poly.new_node(vector(25, +5,z));
  88.   L[28]= poly.new_node(vector(20,+10,z));
  89.   L[29]= poly.new_node(vector(15,+10,z));
  90.  
  91.   poly.new_edge(L[24],L[29]);
  92.   for(i = 25; i<=29; i++) poly.new_edge(L[i], L[i-1]);
  93.  
  94.   L[30]= poly.new_node(vector(40,-20,z));
  95.   L[31]= poly.new_node(vector(50,-20,z));
  96.   L[32]= poly.new_node(vector(55, -5,z));
  97.   L[33]= poly.new_node(vector(65, -5,z));
  98.   L[34]= poly.new_node(vector(70,-20,z));
  99.   L[35]= poly.new_node(vector(80,-20,z));
  100.   L[36]= poly.new_node(vector(65, 20,z));
  101.   L[37]= poly.new_node(vector(55, 20,z));
  102.  
  103.   poly.new_edge(L[30],L[37]);
  104.   for(i = 31; i<=37; i++) poly.new_edge(L[i], L[i-1]);
  105.  
  106.   L[38]= poly.new_node(vector(55, 0,z));
  107.   L[39]= poly.new_node(vector(65, 0,z));
  108.   L[40]= poly.new_node(vector(60,15,z));
  109.   poly.new_edge(L[38],L[40]);
  110.   poly.new_edge(L[39],L[38]);
  111.   poly.new_edge(L[40],L[39]);
  112.  
  113. }
  114.  
  115.  
  116.  
  117. void make_logo(polyhedron& poly)
  118.   node L[41];
  119.   node R[41];
  120.  
  121.   make_side(poly,L,-5);
  122.   make_side(poly,R,+5);
  123.  
  124.   for(int i = 0; i<41; i++) poly.new_edge(L[i],R[i]);
  125.  
  126. }
  127.  
  128.  
  129.  
  130.   
  131.  
  132. main()
  133.   int w,h;
  134.  
  135.   char* map = Read_Leda_Bitmap("../../incl/LEDA/bitmaps/leda.lbm",w,h);
  136.  
  137.   window W(w,h,window::center,window::center);
  138.   W.set_frame_label(
  139.               "The LEDA Platform for Combinatorial and Geometric Computing");
  140.   W.init(-100,100,-100);
  141.   W.set_show_coordinates(false);
  142.   W.insert_bitmap(w,h,map);
  143.   W.set_node_width(4);
  144.   W.set_mode(xor_mode);
  145.  
  146.   node v;
  147.  
  148.   int speed = 30;
  149.  
  150.   // define a polyhedron in 3d space
  151.  
  152.   polyhedron poly;
  153.  
  154.   make_logo(poly);
  155.  
  156.   //W.draw_point(0,0);
  157.  
  158.   forall_nodes(v,poly) rotate(0.3,0.5,poly[v]);
  159.  
  160.   polyhedron poly0 = poly;
  161.  
  162.   vector dir(2);   // direction and speed of rotation
  163.  
  164.   vector trans0(0,0,0);
  165.   vector trans(0,0,0);
  166.  
  167.   draw_poly(W,poly,trans);
  168.   
  169.   for(;;)
  170.   { dir[0] += random(-10000,10000)/1000000.0;
  171.     dir[1] += random(-10000,10000)/1000000.0;
  172.     dir = dir.norm()*(speed/500.0);
  173.  
  174.     //trans[0] += random(-1000,1000)/5000.0;
  175.     //trans[1] += random(-1000,1000)/5000.0;
  176.     //trans[1] += random(-1000,1000)/5000.0;
  177.  
  178.     if (W.get_button() && W.confirm("quit leda demo ?")) break;
  179.  
  180.     forall_nodes(v,poly) rotate(dir[0],dir[1],poly[v]);
  181.  
  182.     draw_poly(W,poly,trans);   // draw new position
  183.     draw_poly(W,poly0,trans0);  // erase old position  (xor_mode !!)
  184.  
  185.     //trans0 = trans;
  186.  
  187.     // poly0 = poly;
  188.     node w = poly.first_node();
  189.     forall_nodes(v,poly0) 
  190.     { poly0[v] = poly[w];
  191.       w = poly.succ_node(w);
  192.      }
  193.   }
  194.  
  195.  return 0;
  196. }
  197.